import Vue from 'vue';
import Vuex from 'vuex';
import httpInterface from "../util/httpInterface";

Vue.use(Vuex)

//创建VueX对象
const store = new Vuex.Store({
  state: {
    page: {
      pageNum: 1,
      pageSize: 100
    },
    tenantId: "828115",
    userInfo: null,
    staffDataInfo: "",
    biaoduanList: [],
    organizations: [],
    group: [],
    role: [],
    marriage: [],
    nature: [],
    bloodTypeInfo: [],
    educationInfo: [],
    politicsStatusInfo: [],
    tenderList: [],
    tender: "",
    workAreaList: [],
    deviceCarList: [],
  },

  mutations: {
    changeTenantId(state, data) {
      state.tenantId = data;
    },

    changeUserInfo(state, data) {
      state.userInfo = data;
    },

    changeStaffInfo(state, data) {
      state.staffDataInfo = data;
    },

    changeTendersInfo(state, data) {
      state.biaoduanList = data;
    },

    changeDutymanageList(state, data) {
      state.organizations = data;
    },

    changeGroupList(state, data) {
      state.group = data;
    },

    changeRoleList(state, data) {
      state.role = data;
    },

    changeMaritalstatusList(state, data) {
      state.marriage = data;
    },

    changeWorknatureList(state, data) {
      state.nature = data;
    },

    changeBloodTypeList(state, data) {
      state.bloodTypeInfo = data;
    },

    changeEdutypeList(state, data) {
      state.educationInfo = data;
    },

    changePoliticsstatusList(state, data) {
      state.politicsStatusInfo = data;
    },

    changeTenderList(state, data) {
      state.tenderList = data;
      state.tender = data[0].organizationUuid;
    },

    changeWorkAreaInfo(state, data) {
      state.workAreaList = data;
    },

    changeDeviceCar(state, data) {
      state.deviceCarList = data;
    }
  },

  actions: {
    // 获取标段信息列表数据
    getTendersInfo(state, callback) {
      var params = { page: 1, size: 100 };
      httpInterface.requestGet(this, httpInterface.URL_BINDSECTION_FINDPAGE, params, function (res) {
        var data = res.data;
        let { success, other } = data;
        if (success) {
          state.commit("changeTendersInfo", other.rows);
          callback && callback(other.rows)
        }
      }, null)
    },

    // 获取组织列表
    getDutymanageList (state) {
      var params = state.state.page;
      httpInterface.requestPost(this, httpInterface.URL_DUTYMANAGE_LIST, params, function (response) {
        var data = response.data;
        if (data.code == '0000') state.commit("changeDutymanageList", data.data);
      }, null);
    },

    // 获取班组列表
    getGroupList(state) {
      var params = state.state.page;
      httpInterface.requestPost(this, httpInterface.URL_GROUP_LIST, params, function (response) {
        var data = response.data;
        state.commit("changeGroupList", data.data);
      }, null);
    },

    // 获取角色
    getRoleList(state) {
      var params = state.state.page;
      httpInterface.requestPost(this, httpInterface.URL_ROLE_LIST, params, function (response) {
        var datas = response.data.data;
        state.commit("changeRoleList", datas);
      });
    },

    //婚姻状况列表
    getMaritalstatusList(state) {
      httpInterface.getMaritalstatusList(function (datas) {
        state.commit("changeMaritalstatusList", datas)
      });
    },

    //用工性质列表
    getWorknatureList(state) {
      httpInterface.getWorknatureList(function (datas) {
        state.commit("changeWorknatureList", datas)
      });
    },

    //血型列表
    getBloodtypeList (state) {
      httpInterface.getBloodtypeList(function (datas) {
        state.commit("changeBloodTypeList", datas);
      });
    },

    //学历列表
    getEdutypeList (state) {
      httpInterface.getEdutypeList(function (datas) {
        state.commit("changeEdutypeList", datas)
      });
    },

    // 政治面貌
    getPoliticsstatusList (state) {
      httpInterface.getPoliticsstatusList(function (datas) {
        state.commit("changePoliticsstatusList", datas)
      });
    },

    // 环境监测数据
    getEnvironmentInfo(state, data) {
      let params = {
        thirdPartyId: data.id
      }
      httpInterface.requestGet(this, httpInterface.URL_ENVIRONMENT_GETLIST, params, (res) => {
        var { other } = res.data;
        state.commit("changeTenderList", other);
        data.fn && data.fn(other);
      },null)
    },

    // 获取环境监测 设备列表
    getEnvironmentDevice(state, data) {
      let params = {
        page: 1,
        size: 1000,
        areaId: data.areaId
      };
      httpInterface.requestPost(this, httpInterface.URL_THIRDPARTY_LIST, params, res => {
        let { other, success } = res.data;
        if (success) {
           if (typeof(data.fn) == 'function') data.fn(other);
        }
      }, null, true)
    },

    // 获取车辆设备列表
    getCarDevice({ state, commit }, data) {
      let params = {
        page: 1,
        size: 1000,
        areaId: data.areaId
      };
      httpInterface.requestPost(this, httpInterface.URL_THIRDPARTY_VEHICLE_LIST, params, res => {
        let { other, success } = res.data;
        if (success) {
          if (typeof(data.fn) == 'function') data.fn(other.rows);
        }
      }, null, true)
    },

    // 获取工区列表
    getWorkAreaInfo({ state, commit }, data) {
      let params = { page: 1, size: 200 };
      httpInterface.requestGet(this, httpInterface.URL_WORKAREA_LIST, params, res => {
        let { other, success } = res.data;
        commit("changeWorkAreaInfo", other.rows)
      }, null)
    },

    // 根据标段 查询所属工区
    selectByBidSection(state, data) {
      let params = { bidSection: data.bidSection };
      httpInterface.requestGet(this, httpInterface.URL_SELECT_BY_BID_SECTION, params, res => {
        let { other, success } = res.data;
        if (success) {
          if (typeof(data.fn) == 'function') data.fn(other);
        }
      }, null)
    }
  }
})

export default store
